const Mock=require('mockjs');
const baseURL='/classroom';
const data={//自己造的假数据，怎么放到response里面？
  pageNum:10,
  pageSize:10,
  list:[]
}
for(let i=0;i<20;i++){
  data.list.push({
    name: Mock.Random.cword(6),
    campus: Mock.Random.cword(4),
    state: Mock.Random.cword(2),
  })
}
module.exports = [
  {
    url:baseURL+'/getListByCondition',//下次不要把前缀和getList写得一样，不然的话，如果放在/getListByCondition后面，那么返回的就是getList的response
    type:'get',
    response: config=>{

      const {name,time,page = 1, limit = 20}=config.query;//不要忘记.query
      var data2=[];
      if(name!=''){
        for(var i=0;i<data.list.length;i++){
          if(data.list[i].name.includes(name)){
            data2.push(data.list[i]);
          }
        }
      }
      const data3=data.list.filter((item,index)=>{
        return limit*page>index && limit*(page-1)<=index;
      })
      return{
        code:20000,
        data,
        data2:data2,
        data3:data3
      }
    }
  },
  {
    url:baseURL+'/getList',
    type:'get',
    response: config=>{
      // cosnt
      // const dataList=data.list.filter((item,index)=>{
      //   return limit*page>index && limit*(page-1)<=index;
      // })
      return{
        code:20000,
        data:data,
        // list:dataList
      }
    }
  },
  {
    url:baseURL+'/add',
    type:'post',
    response: config => {
      let obj=config.body;
      data.list.push(obj);
      return{
        code: 20000,
        data: obj
      }
    }
  },
  {
    url:baseURL+'/delete',
    type:'delete',
    response: config => {
      let index=config.query.index;
      data.list.splice(index,1);
      return{
        code: 20000,
        data: {
          index: index,
          leftDataListSize: data.list.length
        }
      }
    }
  },
  {
    url:baseURL+'/put',
    type:'put',
    response: config=>{
     const {name,campus,state}=config.body;
      var i;
      for( i=0;i<data.list.length;i++){
        if(data.list[i].name==name){
          data.list[i].campus=campus;
          data.list[i].state=state;
          break;
        }
      }
      return{
        code:20000,
        data:data.list[i]
      }
    }
  },

]
